package com.ddwl.shop.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.common.exception.CommonException;
import com.ddwl.common.exception.ParamException;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.shop.*;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.shop.ShopAccountVo;
import com.ddwl.schema.vo.shop.ShopSupplyVo;
import com.ddwl.schema.vo.shop.ShopVo;
import com.ddwl.shop.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author Fu Zhaohui
 * @version 1.0.0.RELEASE
 * @date 2020/3/5 10:59 上午
 */
@Api(tags = "门店服务")
@RestController
@RequestMapping("/shop")
public class ShopController extends BaseController {

    @Resource
    private ShopService shopService;

    @PostMapping("/createShop")
    @ApiOperation("创建门店")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo createShop(@Valid @RequestBody ShopBo bo) {
        shopService.createShop(bo,getUserId());
        return RestVo.ok();
}

    @PostMapping("/updateShop")
    @ApiOperation("修改门店 多典后台")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo updateShop(@Valid @RequestBody ShopBo bo) {
        shopService.update(bo,getUserId());
        return RestVo.ok();
    }

    @PostMapping("/updateShopForShopConsole")
    @ApiOperation("修改门店 商家后台")
    @Auth(product = BaseEnum.ProductEnum.SHOP_CONSOLE)
    public RestVo updateShopForShopConsole(@Valid @RequestBody ShopUpdateBo bo) {
        bo.setId(getShopId());
        shopService.updateShop(bo,getUserId());
        return RestVo.ok();
    }

    @PostMapping("/updateShopForWBMConsole")
    @ApiOperation("修改门店 wbm后台")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo updateShopForWBMConsole(@Valid @RequestBody ShopUpdateBo bo) {
        ParamException.isBlank(bo.getId(),"门店id为空");
        ShopVo shopVo = getShopVo(bo.getId());
        // String id = shopService.getShopIdByUserId(getUserId(),getFenXiaoId());
        bo.setId(shopVo.getId());
        shopService.updateShop(bo,getUserId());
        return RestVo.ok();
    }

    @PostMapping("/updateShopNameForWBMConsole")
    @ApiOperation("修改门店名称 wbm后台")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo updateShopNameForWBMConsole(@Valid @RequestBody ShopUpdateBo bo) {
        ParamException.isBlank(bo.getId(),"门店id为空");
        ShopVo shopVo = getShopVo(bo.getId());
        bo.setId(shopVo.getId());
        shopService.updateShopName(bo,getUserId());
        return RestVo.ok();
    }

    @GetMapping("/queryShopForWBMConsole")
    @ApiOperation("查询门店 wbm后台")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<ShopVo> queryShopForWBMConsole() {
        ShopVo shopVo = shopService.getShopByUserIdApi(getUserId());
        return RestVo.ok(shopVo);
    }

    @PostMapping("/queryShopForWBMConsoleNew")
    @ApiOperation("查询门店 wbm后台")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<ShopVo> queryShopForWBMConsole(@RequestParam String shopId) {
        ShopVo shopVo1 = getShopVo(shopId);
        ShopVo shopVo = shopService.getShopByOpenId(shopVo1.getId());
        return RestVo.ok(shopVo);
    }

    @PostMapping("/queryShopByOpenIdApi")
    @ApiOperation("查询门店")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<ShopVo> queryShopByOpenIdApi(@RequestParam String shopId) {
        ShopVo shopVo = shopService.getShopByOpenId(shopId);
        return RestVo.ok(shopVo);
    }

    @PostMapping("/updateShopForWBMNotice")
    @ApiOperation("修改门店公告 wbm后台")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo updateShopForWBMNotice(@RequestParam String notice) {
        String id = shopService.getShopIdByUserId(getUserId());
        shopService.updateShopNotice(notice,id);
        return RestVo.ok();
    }

    @PostMapping("/updateShopForWBMNoticeOpenId")
    @ApiOperation("修改门店公告 wbm后台")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo updateShopForWBMNotice(@RequestParam String notice,@RequestParam String shopId) {
        shopService.updateShopNotice(notice,shopId);
        return RestVo.ok();
    }

    @PostMapping("/updateShopForWBMStatus")
    @ApiOperation("修改门店经营状态wbm后台")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo updateShopForWbmStatus(@RequestParam Integer status) {
        String id = shopService.getShopIdByUserId(getUserId());
        shopService.updateShopStatus(status,id);
        return RestVo.ok();
    }

    @PostMapping("/updateShopForWBMStatusOpenApi")
    @ApiOperation("修改门店经营状态wbm后台")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo updateShopForWbmStatus(@RequestParam Integer status,@RequestParam String shopId) {
        getShopVo(shopId);
        shopService.updateShopStatus(status,shopId);
        return RestVo.ok();
    }

    @PostMapping("/stopShop")
    @ApiOperation("强制禁用门店")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo stopShop(@RequestParam String shopId,@RequestParam Integer systemStop) {
        shopService.stopShop(shopId,systemStop,getUserId());
        return RestVo.ok();
    }

    @DeleteMapping("/deleteShop")
    @ApiOperation("删除门店")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo deleteShop(@RequestParam String shopId) {
        shopService.deleteShop(shopId,getUserId());
        return RestVo.ok();
    }

    @PostMapping("/getShopDetail")
    @ApiOperation("获取门店明细 多典后台")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    public RestVo<ShopVo> detail(@RequestBody QueryShopBo bo) {
        return RestVo.ok(shopService.getDetailConsole(bo));
    }

    @GetMapping("/getShopDetailConsole")
    @ApiOperation("获取门店明细 商家后台")
    @Auth(product = BaseEnum.ProductEnum.SHOP_CONSOLE)
    public RestVo<ShopVo> getShopDetailConsole() {
        return RestVo.ok(shopService.getByIdShopConsole(getShopId()));
    }

    @GetMapping(value ={"/getShopDetail/{id}","/getShopDetail"})
    @ApiOperation("获取门店明细 小程序")
    @Auth(required=false)
    public RestVo<ShopVo> detail(@PathVariable(required = false) String id) {
        return RestVo.ok(shopService.getById(id,getUserId(),getUserNameNew()));
    }

    @GetMapping(value ="/getDefaultShop")
    @ApiOperation("获取默认门店 小程序")
    @Auth(required=false)
    public RestVo<ShopVo> getDefaultShop() {
        return RestVo.ok(shopService.getDefaultShop());
    }

    @PostMapping("/getShopPage")
    @ApiOperation("分页查询门店 后台")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    public RestVo<PageVo<ShopVo>> getShopPage(@Valid @RequestBody PageBo<QueryShopBo> pageBo) {
        pageBo.getParam().setApprove(Boolean.TRUE);
        return RestVo.ok(shopService.getShopList(pageBo));
    }

    @PostMapping("/getShopPageForApi")
    @ApiOperation("分页查询门店 api")
    @Auth(product = {BaseEnum.ProductEnum.API})
    public RestVo<PageVo<ShopVo>> getShopPageForApi(@Valid @RequestBody PageBo<QueryShopBo> pageBo) {
        return RestVo.ok(shopService.getShopList(pageBo));
    }

    @PostMapping("/getShops")
    @ApiOperation("查询门店list api")
    @Auth(product = {BaseEnum.ProductEnum.API})
    public RestVo<List<ShopVo>> getShops(@RequestBody QueryShopBo pageBo) {
        return RestVo.ok(shopService.getShops(pageBo));
    }

    @GetMapping("/getShopForApi/{id}")
    @ApiOperation("获取门店明细 api")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<ShopVo> getShopForApi(@PathVariable String id) {
        return RestVo.ok(shopService.getShopForApi(id));
    }

    /**
     * 查询门店
     * @param id
     * @return
     */
    @GetMapping("/getShopById")
    @ApiOperation("查询门店信息 api")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<ShopVo> getShopById(@RequestParam String id){
        return RestVo.ok(shopService.getShopById(id));
    }

    /**
     * 根据店铺id列表查询店铺列表
     * @param shopIds
     * @return
     */
    @PostMapping("/getShopListByIds")
    @ApiOperation("根据店铺id列表查询店铺列表")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<List<ShopVo>> getShopListByIds(@RequestBody Collection<String> shopIds){
        return RestVo.ok(shopService.getShopListByIds(shopIds));
    }

    @ApiOperation("根据用户获取门店明细 api")
    @Auth(product = BaseEnum.ProductEnum.API)
    @GetMapping("/getShopByUserId/{userId}")
    public RestVo<ShopVo> getShopByUserId(@PathVariable String userId){
        return RestVo.ok(shopService.getShopByUserIdApi(userId));
    }

    /**
     * 根据用户id获取简单店铺信息
     * @param userId
     * @return
     */
    @GetMapping("/getSimpleShopByUserId/{userId}")
    @ApiOperation("根据用户id获取简单店铺信息")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<ShopVo> getSimpleShopByUserId(@PathVariable String userId){
        return RestVo.ok(shopService.getSimpleShopByUserId(userId));
    }


    @PostMapping("/getSimpleShopByUserIds")
    @ApiOperation("批量获取用户开的店铺")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<List<ShopVo>> getSimpleShopByUserIds(@RequestBody List<String> userIds){
        return RestVo.ok(shopService.getSimpleShopByUserIds(userIds));
    }

    /**
     * 根据用户id和店铺id判断用户是否该店铺的管理员
     * @param userId
     * @param shopId
     * @return
     */
    @GetMapping("/judgeUserHasShop")
    @ApiOperation("根据用户id和店铺id判断用户是否该店铺的管理员")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<ShopVo> judgeUserHasShop(@RequestParam("userId") String userId,@RequestParam(value = "shopId") String shopId){
        return RestVo.ok(shopService.judgeUserHasShop(userId,shopId));
    }

    /**
     * 查询登录人所在门店
     * @param
     * @return
     */
    @GetMapping("/getUserOwnShopByUserId")
    @ApiOperation("查询登录人所在门店")
    public RestVo<String> getUserOwnShopByUserId(){
        return RestVo.ok(shopService.getUserShopByUserId(getUserId()));
    }

    /**
     * 查询登录人所在门店
     * @param
     * @return
     */
    @GetMapping("/getUserOwnShopByUser")
    @ApiOperation("查询登录人所在门店")
    public RestVo<ShopVo> getUserOwnShopByUser(){
        return RestVo.ok(shopService.getSimpleShopByUserId(getUserId()));
    }

    /**
     * 查询默认门店
     * @return
     */
    @ApiOperation("获取默认门店 api")
    @Auth(product = BaseEnum.ProductEnum.API)
    @GetMapping("/getDefaultShopForApi")
    public RestVo<ShopVo> getDefaultShopForApi(){
        return RestVo.ok(shopService.getShopByUserIdApi());
    }

    @ApiOperation("根据手机号查询门店账号信息")
    @Auth(product = BaseEnum.ProductEnum.API)
    @GetMapping("/getUserShopByMobile")
    public RestVo<ShopAccountVo> getUserShop(String mobile){
        return RestVo.ok(shopService.getShopByMobile(mobile));
    }

    @ApiOperation("记录登录门店信息")
    @GetMapping("/shopUserRecord")
    public RestVo getShopUserRecord(String shopId){
        shopService.saveShopUserRecord(shopId,getUserId(),getUserNameNew());
        return RestVo.ok();
    }

    @PostMapping("/getByIds")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE,BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("根据id列表获取门店信息")
    public RestVo<Map<String, ShopSupplyVo>> getByIds(@RequestBody List<String> ids){
        return RestVo.ok(shopService.getByIds(ids));
    }

    @GetMapping("/getWxaCode")
    @ApiOperation("获取店铺小程序码")
    public RestVo<String> getWxaCode(@RequestParam String shopId){
        return RestVo.ok(shopService.getWxaCodeOSSUrl(shopId));
    }

    @PostMapping("/getNearShop")
    @ApiOperation("根据经纬度查询附近门店信息")
    @Auth(required = false)
    public RestVo<PageVo<ShopVo>> getNearShop(@Valid @RequestBody PageBo<QueryNearShopBo> queryNearShopBo){
        QueryNearShopBo param = Optional.ofNullable(queryNearShopBo.getParam()).orElse(new QueryNearShopBo());
        if(param.getLat() == null || param.getLng() == null) {
            throw new CommonException("经纬度为空");
        }
        return RestVo.ok(shopService.getNearShop(queryNearShopBo));
    }

    @PostMapping("/getResentShop")
    @ApiOperation("根据最近浏览店铺")
    @Auth(required = false)
    public RestVo<PageVo<ShopVo>> getResentShop(@RequestBody PageBo<QueryResentShopBo> queryResentShopBo){
        return RestVo.ok(shopService.getResentShop(queryResentShopBo, getUserId()));
    }

    @PostMapping("/searchShopForWbm")
    @ApiOperation("搜寻店铺 wbm")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<PageVo<ShopVo>> searchShopForWbm(@RequestBody PageBo<SearchShopBo> bo){
        return RestVo.ok(shopService.searchShopForWbm(bo, getUserId()));
    }

    @PostMapping("/searchShopAll")
    @ApiOperation("搜寻店铺")
    @Auth(required = false)
    public RestVo<PageVo<ShopVo>> searchShopAll(@RequestBody PageBo<SearchShopBo> bo){
        return RestVo.ok(shopService.searchShopForWbm(bo,getUserId()));
    }

    @PostMapping("/searchShop")
    @ApiOperation("搜索店铺 多典后台")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    public RestVo<List<ShopVo>> searchShop(@RequestBody SearchShopBo bo){
        return RestVo.ok(shopService.searchShop(bo));
    }

    @PostMapping("/createWxaCode")
    @ApiOperation("生成带logo的小程序码")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    public RestVo createLogoWxaCode(){
        shopService.createWxaCode();
        return RestVo.ok();
    }

    @PostMapping("/agent/page/{itemId}")
    @ApiOperation("分页查询代理商品的店铺")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    public RestVo<PageVo<ShopVo>> agentShopPage(@Valid @RequestBody PageBo pageBo, @PathVariable String itemId) {
        return RestVo.ok(shopService.agentShopPage(pageBo,itemId));
    }

}
